package com.ys.malini.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ys.malini.dao.ICommodityDao;
import com.ys.malini.dao.IProductDao;
import com.ys.malini.model.TOrder;
import com.ys.malini.model.TProduct;

/**
 * 对购物车的相关操作，
 * 购物车放在SESSION中，不入库，只针对购物车中map进行增删操作
 * @author yang.wang
 *
 */
@SuppressWarnings( { "unused", "unchecked", "unchecked" })
public class CartOperateAction extends DispatchAction {
	
	ICommodityDao commodityDao;
	

	public void setCommodityDao(ICommodityDao commodityDao) {
		this.commodityDao = commodityDao;
	}

	public CartOperateAction() {
		super();
	}

	/**
	 * 将商品添加至购物车
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("null")
	public ActionForward addToCart(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String productId = request.getParameter("productId");
		String smallImages = request.getParameter("smallimages");
		List list = commodityDao.findComInfoById(productId);
		Object[] object = (Object[]) list.get(0);
		List orderList = (List) request.getSession().getAttribute("orderList");
		if(orderList==null){
			orderList = new ArrayList();
			TOrder order = new TOrder();
			order.setProductId((Integer)object[0]);
			order.setProductName((String)object[1]);
			order.setColor((String)object[2]);
			order.setSize((String)object[3]);
			order.setSalePrice((Double)object[4]);
			order.setMarketPrice((Double)object[5]);
			order.setBuyCount(1);
			order.setSmallImages((String)object[6]);
			
			//order.setSmallImages(smallImages);
			orderList.add(order);
		}else{
			for(int i=0;i<orderList.size();i++){
				TOrder order = (TOrder) orderList.get(i);
				if(productId.equals(order.getProductId())){
					order.setBuyCount(order.getBuyCount()+1);
					order.setProductName((String)object[1]);
					order.setColor((String)object[2]);
					order.setSize((String)object[3]);
					order.setSalePrice((Double)object[4]);
					order.setMarketPrice((Double)object[5]);
					order.setSmallImages(smallImages);
					orderList.add(order);
				}
			}
		}
		request.getSession().setAttribute("orderList", orderList);
		request.setAttribute("addToCartMsg", "添加成功");
		return mapping.findForward("viewCart");
	}
	
	/**
	 * 查看购物车中所有订单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward viewCart(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String productId = request.getParameter("productId");
		List cartList = (List) request.getSession().getAttribute("orderList");
		if(cartList==null){
			cartList = new ArrayList();
		}else{
			cartList.add(productId);
		}
		
		return mapping.findForward("viewCart");
	}

	/**
	 * 从购物车中删除订单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward deleteOrderFromCart(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String productId = request.getParameter("productId");
		List orderList = (List) request.getSession().getAttribute("orderList");
		for(int i=0;i<orderList.size();i++){
			TOrder order = (TOrder) orderList.get(i);
			if(productId.equals(order.getProductId())){
				orderList.remove(order);
			}
		}
		request.getSession().setAttribute("orderList", orderList);
		request.setAttribute("cartMsg", "删除成功");
		return mapping.findForward("viewCart");
	}
	
	/**
	 * 结算购物车中订单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward settlement(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("settlementSuccess");
	}
}
